package persentation.presentUniversityDean;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import businesslogic.userBl.UniversityDeanController;
import dataservice.ResultMessage;

public class ChangePasswordPanel extends JPanel {
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;
	private UniversityDeanController uDeanController;
	private static Image INFO = new ImageIcon("Graphics/universityDean/info.png").getImage();

	/**
	 * Create the panel.
	 */
	public ChangePasswordPanel(UniversityDeanController uDeanController) {
		setSize(777, 600);
		setLayout(null);
		this.uDeanController = uDeanController;
		JLabel lblNewLabel = new JLabel("输入新密码");
		lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
		lblNewLabel.setBounds(272, 152, 170, 34);
		add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("确认新密码");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 18));
		lblNewLabel_1.setBounds(272, 196, 155, 49);
		add(lblNewLabel_1);

		JButton button = new JButton("确认");
		button.setFont(new Font("微软雅黑", Font.BOLD, 14));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(null, "确认修改密码？", "确认",
						JOptionPane.YES_NO_OPTION);
				if (i == JOptionPane.YES_OPTION) {
					if (!isSamePassword(passwordField_1.getPassword(),
							passwordField_2.getPassword())) {
						JOptionPane.showMessageDialog(null, "两次输入密码不同", "失败！",
								JOptionPane.ERROR_MESSAGE);
					} else {
						if (changepassword()) {
							JOptionPane.showMessageDialog(null, "修改密码成功！",
									"成功！", JOptionPane.INFORMATION_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(null, "原密码错误！",
									"失败！", JOptionPane.ERROR_MESSAGE);
						}
					}
				}

			}
		});
		button.setBounds(272, 290, 93, 35);
		add(button);

		JButton button_1 = new JButton("重置");
		button_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				passwordField_1.setText(null);
				passwordField_2.setText(null);
			}
		});
		button_1.setBounds(476, 290, 93, 35);
		add(button_1);

		JLabel label = new JLabel("原密码");
		label.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label.setBounds(272, 99, 104, 55);
		add(label);

		passwordField = new JPasswordField();
		passwordField.setBounds(452, 118, 128, 21);
		add(passwordField);

		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(452, 161, 128, 21);
		add(passwordField_1);

		passwordField_2 = new JPasswordField();
		passwordField_2.setBounds(452, 212, 128, 21);
		add(passwordField_2);
	}

	private boolean isSamePassword(char[] first, char[] second) {
		if (first.length != second.length) {
			return false;
		} else {
			for (int i = 0; i < second.length; i++) {
				if (first[i] != second[i]) {
					return false;
				}
			}
			return true;
		}
	}

	private boolean changepassword() {
		ResultMessage resultMessage = uDeanController.changePassword(
				passwordField.getPassword(), passwordField_1.getPassword(),
				passwordField_2.getPassword());

		if (resultMessage == ResultMessage.MODIFY_SUCCESS) {
			return true;
		}
		return false;
	}

	public void paintComponent(Graphics g) {
		g.drawImage(INFO, 0, 0, null);
	}
}
